package com.jqutil.jqutil.cy.io;

import com.alibaba.excel.EasyExcel;
import com.jqutil.jqutil.cy.common.Constant;
import com.jqutil.jqutil.cy.entity.ReadData;
import com.jqutil.jqutil.cy.entity.Write2Data;
import com.jqutil.jqutil.cy.entity.WriteData;
import com.jqutil.jqutil.cy.utils.DataUtils;

import java.util.*;

/**
 * @Author: chenyun
 * @Date: 2021/09/08/14:06
 * @Description:
 */

//写数据类
public class Write {

    /**
     * 将转换格式后的数据写入指定的表
     * @param readDataList  原始表格转换后的数据
     * @param path      原始表格路径  也即读取表格路径
     */
    public static void write(List<ReadData> readDataList, String path){

        StringBuffer buffer = new StringBuffer();
        int index1 = path.lastIndexOf(".xls");
        int index2 = path.lastIndexOf(".xlsx");
        if(index1>0){
            buffer.append(path.substring(0,index1));
            buffer.append("_副本.xls");
        }
        if(index2>0){
            buffer.append(path.substring(0,index2));
            buffer.append("_副本.xlsx");
        }

//        DataUtils.parseReadDataListToWriteData(readDataList);
//
//        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
//        // 如果这里想使用03 则 传入excelType参数即可
//        List<WriteData> writeDataList = DataUtils.parseReadDataListToWriteData(readDataList);
//        System.out.println("#############################");
//        for (WriteData writeData : writeDataList) {
//            System.out.println(writeData);
//        }
//        System.out.println("#################################");
//        ArrayList<Write2Data> write2DataList = new ArrayList<>();
//        for (WriteData writeData : writeDataList) {
//            Write2Data write2Data = new Write2Data();
//            write2Data.setWeekCount(writeData.getWeekCount());
//            write2Data.setCourseNum(writeData.getCourseNum());
//            if(null !=writeData.getOne()){
//                write2Data.setOne(writeData.getOne().toString());
//            }
//            if(null !=writeData.getTwo()){
//                write2Data.setTwo(writeData.getTwo().toString());
//            }
//            if(null !=writeData.getThree()){
//                write2Data.setThree(writeData.getThree().toString());
//            }
//            if(null !=writeData.getFour()){
//                write2Data.setFour(writeData.getFour().toString());
//            }
//            if(null !=writeData.getFive()){
//                write2Data.setFive(writeData.getFive().toString());
//            }
//            if(null !=writeData.getSex()){
//                write2Data.setSex(writeData.getSex().toString());
//            }
//            if(null !=writeData.getSeven()){
//                write2Data.setSeven(writeData.getSeven().toString());
//            }
//        }

        EasyExcel.write(String.valueOf(buffer), Write2Data.class).sheet("Sheet1").doWrite(dataParse(readDataList));
        for (ReadData readData : readDataList) {
            System.out.println(readData);
        }
    }


    private static List<WriteData> data(List<ReadData> readDataList) {
        List<WriteData> list = new ArrayList<WriteData>();
        List<Integer> weekRange = DataUtils.weekRange();
        int maxWeek = weekRange.get(weekRange.size()-1);
        for (int j = 1;j<=maxWeek;j++){
            for (int i = 1; i <= 5; i++) {
                WriteData data = new WriteData("第"+j+"周",Constant.COURSENUM_MAP2.get(i));
                HashSet<String> one = new HashSet<String>();
                HashSet<String> two = new HashSet<String>();
                HashSet<String> three = new HashSet<String>();
                HashSet<String> four = new HashSet<String>();
                HashSet<String> five = new HashSet<String>();
                HashSet<String> six = new HashSet<String>();
                HashSet<String> seven = new HashSet<String>();
                // 填充数据的逻辑
                for (ReadData readData : readDataList) {
                    Set<String> classNameKey = readData.getClassNameAndListWeek().keySet();
                    Iterator<String> it = classNameKey.iterator();
                    // 遍历Key  也就是教室名字
                    while (it.hasNext()) {
                        String key = it.next();
                        // 根据key遍历value
                        List<String> values = readData.getClassNameAndListWeek().get(key);
                        Iterator<String> itValue = values.iterator();
                        while (itValue.hasNext()) {
                            // 遍历出来的周数
                            String value = itValue.next();
                            // 假如 周数等于j   节次等于i  则对周一至周日赋值
                            if((Integer.valueOf(value)==(j)) && (Constant.COURSENUM.get(readData.getCourseNum())==i)){
                               // 星期一
                                if(readData.getWeek().equals(Constant.weekList.get(0))){
                                   one.add(key);
                                }
                                // 星期二
                                if(readData.getWeek().equals(Constant.weekList.get(1))){
                                    two.add(key);
                                }
                                // 星期三
                                if(readData.getWeek().equals(Constant.weekList.get(2))){
                                    three.add(key);
                                }
                                // 星期四
                                if(readData.getWeek().equals(Constant.weekList.get(3))){
                                    four.add(key);
                                }
                                // 星期五
                                if(readData.getWeek().equals(Constant.weekList.get(4))){
                                    five.add(key);
                                }
                                // 星期六
                                if(readData.getWeek().equals(Constant.weekList.get(5))){
                                    six.add(key);
                                }
                                // 星期日
                                if(readData.getWeek().equals(Constant.weekList.get(6))){
                                    seven.add(key);
                                }

                            }

                        }
                    }
                }
                data.setOne(one);
                data.setTwo(two);
                data.setThree(three);
                data.setFour(four);
                data.setFive(five);
                data.setSex(six);
                data.setSeven(seven);
                list.add(data);
            }
        }
        return list;
    }


    private static List<Write2Data> dataParse(List<ReadData> readDataList){
        List<WriteData> writeDataList = data(readDataList);
        ArrayList<Write2Data> write2DataList = new ArrayList<>();
        for (WriteData writeData : writeDataList) {
            Write2Data write2Data = new Write2Data();
            write2Data.setWeekCount(writeData.getWeekCount());
            write2Data.setCourseNum(writeData.getCourseNum());
            int oneLength = writeData.getOne().toString().length();
            write2Data.setOne(writeData.getOne().toString().substring(1,oneLength-1).replace(",","/"));

            int twoLength = writeData.getTwo().toString().length();
            write2Data.setTwo(writeData.getTwo().toString().substring(1,twoLength-1).replace(",","/"));

            int threeLength = writeData.getThree().toString().length();
            write2Data.setThree(writeData.getThree().toString().substring(1,threeLength-1).replace(",","/"));

            int fourLength = writeData.getFour().toString().length();
            write2Data.setFour(writeData.getFour().toString().substring(1,fourLength-1).replace(",","/"));

            int fiveLength = writeData.getFive().toString().length();
            write2Data.setFive(writeData.getFive().toString().substring(1,fiveLength-1).replace(",","/"));

            int sixLength = writeData.getSex().toString().length();
            write2Data.setSex(writeData.getSex().toString().substring(1,sixLength-1).replace(",","/"));

            int sevenLength = writeData.getSeven().toString().length();
            write2Data.setSeven(writeData.getSeven().toString().substring(1,sevenLength-1).replace(",","/"));
            write2DataList.add(write2Data);
        }
        return write2DataList;
    }



}
